/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.v1;

import it.gmariotti.cardslib.demo.CustomHeaderInnerCard;
import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.cards.CustomExpandCard;
import it.gmariotti.cardslib.demo.cards.CustomHeaderExample1;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardExpand;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.view.CardView;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ScrollView;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;


/**
 * Card Header Examples
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class HeaderFragment extends MaterialV1Fragment {

    protected ScrollView mScrollView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_header);
        mScrollView = (ScrollView) this.findComponentById(ResourceTable.Id_card_scrollview);

        initCards();
    }

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_cardheadersubtitle;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_cardheader;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }


    private void initCards() {
        init_standard_header_without_buttons();
        init_standard_header_with_overflow_button();
        init_standard_header_with_overflow_button_dynamic_menu();
        init_standard_header_with_overflow_button_dynamic_menu_without_xml();
        init_standard_header_with_expandcollapse_button();
        init_standard_header_with_expandcollapse_button_custom_area();
        init_standard_header_with_custom_other_button();
        init_standard_header_with_custom_other_button_programmatically();
        init_header_with_custom_inner_layout();
        init_header_with_custom_layout();
        init_header_buttonleft();
    }


    /**
     * This method builds a standard header without buttons
     */
    private void init_standard_header_without_buttons() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        card.addCardHeader(header);

        //Set card in the CardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_std);
        cardView.setCard(card);
    }


    /**
     * This method builds a standard header with overflow button
     */
    private void init_standard_header_with_overflow_button() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Add a popup menu. This method set OverFlow button to visible
        //todo
//        header.setPopupMenu(R.menu.popupmain, new CardHeader.OnClickCardHeaderPopupMenuListener() {
//            @Override
//            public void onMenuItemClick(BaseCard card, MenuItem item) {
//                Toast.makeText(this, "Click on " + item.getTitle(), Toast.LENGTH_SHORT).show();
//            }
//        });
        card.addCardHeader(header);

        //Set card in the CardView
        CardView CardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_overflow);
        CardView.setCard(card);
    }

    /**
     * This method builds a standard header with overflow button with a dynamic menu
     */
    private void init_standard_header_with_overflow_button_dynamic_menu() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Add a popup menu. This method set OverFlow button to visible
        //todo
//        header.setPopupMenu(R.menu.popupmain, new CardHeader.OnClickCardHeaderPopupMenuListener() {
//            @Override
//            public void onMenuItemClick(BaseCard card, MenuItem item) {
//                Toast.makeText(this, "Click on " + item.getTitle(), Toast.LENGTH_SHORT).show();
//            }
//        });

        //Add a PopupMenuPrepareListener to add dynamically a menu entry
        //it is optional.
        //todo
//        header.setPopupMenuPrepareListener(new CardHeader.OnPrepareCardHeaderPopupMenuListener() {
//            @Override
//            public boolean onPreparePopupMenu(BaseCard card, PopupMenu popupMenu) {
//                popupMenu.getMenu().add("Dynamic item");
//
//                //You can remove an item with this code
//                //popupMenu.getMenu().removeItem(ResourceTable.Id_action_settings);
//
//                //return false; You can use return false to hidden the button and the popup
//
//                return true;
//            }
//        });
        card.addCardHeader(header);

        //Set card in the CardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_overflow_dynamic);
        cardView.setCard(card);
    }

    /**
     * This method builds a standard header with overflow button with a dynamic menu
     */
    private void init_standard_header_with_overflow_button_dynamic_menu_without_xml() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Add a popup menu. This method set OverFlow button to visible
        header.setButtonOverflowVisible(true);
        //todo
//        header.setPopupMenuListener(new CardHeader.OnClickCardHeaderPopupMenuListener() {
//            @Override
//            public void onMenuItemClick(BaseCard card, MenuItem item) {
//                Toast.makeText(this, "Click on " + item.getTitle() + "-" + ((Card) card).getCardHeader().getTitle(), Toast.LENGTH_SHORT).show();
//            }
//        });

        //Add a PopupMenuPrepareListener to add dynamically a menu entry
        //it is optional.
        //todo
//        header.setPopupMenuPrepareListener(new CardHeader.OnPrepareCardHeaderPopupMenuListener() {
//            @Override
//            public boolean onPreparePopupMenu(BaseCard card, PopupMenu popupMenu) {
//                popupMenu.getMenu().add("Dynamic item");
//
//                //You can remove an item with this code
//                //popupMenu.getMenu().removeItem(ResourceTable.Id_action_settings);
//
//                //return false; You can use return false to hidden the button and the popup
//
//                return true;
//            }
//        });
        card.addCardHeader(header);

        //Set card in the CardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_overflow_dynamic2);
        cardView.setCard(card);
    }

    /**
     * This method builds a standard header with base expand/collpase
     */
    private void init_standard_header_with_expandcollapse_button() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set visible the expand/collapse button
        header.setButtonExpandVisible(true);

        //Add Header to card
        card.addCardHeader(header);

        //This provides a simple (and useless) expand area
        CardExpand expand = new CardExpand(this);
        //Set inner title in Expand Area
        try {
            expand.setTitle(getResourceManager().getElement(ResourceTable.String_demo_expand_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        card.addCardExpand(expand);

        //Set card in the CardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_expand);
        cardView.setCard(card);
    }

    /**
     * This method builds a standard header with a custom expand/collpase
     */
    private void init_standard_header_with_expandcollapse_button_custom_area() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set visible the expand/collapse button
        header.setButtonExpandVisible(true);

        //Add Header to card
        card.addCardHeader(header);

        //This provides a simple (and useless) expand area
        CustomExpandCard expand = new CustomExpandCard(this);
        //Add Expand Area to Card
        card.addCardExpand(expand);

        //Set card in the CardView
        final CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_expand_custom_area);

        // It is not required.
        card.setOnExpandAnimatorEndListener(new Card.OnExpandAnimatorEndListener() {
            @Override
            public void onExpandEnd(Card card) {
                //TODO: check if hidden area is visible and it would be better an animator to do this
            }
        });

        cardView.setCard(card);
    }

    /**
     * This method builds a standard header with other button visible
     */
    private void init_standard_header_with_custom_other_button() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set visible the expand/collapse button
        header.setOtherButtonVisible(true);

        //Add a callback
        header.setOtherButtonClickListener((card1, view) -> new ToastDialog(getContext()).setText("Click on Other Button").show());

        //Add Header to card
        card.addCardHeader(header);

        //Set card in the CardView
        final CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_other_button);

        cardView.setCard(card);

    }

    /**
     * This method builds a standard header with other button visible which drawable is defined programmatically
     */
    private void init_standard_header_with_custom_other_button_programmatically() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set visible the expand/collapse button
        header.setOtherButtonVisible(true);

        //Add a callback
        header.setOtherButtonClickListener((card1, view) -> new ToastDialog(getContext()).setText("Click on Other Button").show());

        //Use this code to set your drawable
        header.setOtherButtonDrawable(ResourceTable.Graphic_card_menu_button_other_add);

        //Add Header to card
        card.addCardHeader(header);

        //Set card in the CardView
        final CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_other_button_programmatically);

        cardView.setCard(card);

    }


    /**
     * This method builds a header with custom inner layout
     */
    private void init_header_with_custom_inner_layout() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CustomHeaderInnerCard header = new CustomHeaderInnerCard(this);

        //Add Header to card
        card.addCardHeader(header);

        //Set card in the CardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_inner);

        cardView.setCard(card);
    }

    /**
     * This method builds a header with full custom layout
     */
    private void init_header_with_custom_layout() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CustomHeaderExample1 header = new CustomHeaderExample1(this);

        //Add Header to card
        card.addCardHeader(header);

        //Set card in the CardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_layout);

        cardView.setCard(card);
    }

    /**
     * This method builds a header with full custom layout, with buttons on the left
     */
    private void init_header_buttonleft() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this, ResourceTable.Layout_carddemo_buttonleft_inner_header);
        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        header.setOtherButtonVisible(true);
        header.setOtherButtonClickListener((card1, view) -> {
            //Example to change dinamically the button resources
            card1.getCardHeader().setOtherButtonDrawable(ResourceTable.Graphic_card_menu_button_other_add);
            card1.getCardView().refreshCard(card1);
        });
        card.addCardHeader(header);

        //Set card in the CardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_header_buttonleft);
        cardView.setCard(card);
    }

}
